//
//  RequestStatusView.m
//  Feeder
//
//  Created by Diego Acosta on 8/19/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "RequestStatusView.h"

#define kRequestStatusObjectGap 5


@interface RequestStatusView(Private)

- (void)_draw;
- (void)_addItem:(UIView *)view;
- (void)_removeAllItems;
- (void)_resizeView;
- (void)_resizeLabel:(UILabel *)label;
- (void)_customizeLabel:(UILabel *)label;

@end

@implementation RequestStatusView

- (id)init {
	
	// default frame size.
	CGRect frame = CGRectMake(0.0, 0.0, 180.0, 21.0);
	
    if ((self = [super initWithFrame:frame])) {
		
        // Initialization code
		_status = RequestStatusIdle;
		_message = [[NSString alloc] initWithString:@""];
    }
	
	// default values.
	self.backgroundColor = [UIColor clearColor];
	
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark -
#pragma mark Public methods

- (void)setRequestStatus:(RequestStatus)status {

	// check if we're not currently on that status.
	if (status != _status) {
		
		// set status first.
		_status = status;
		
		// remove previous added views.
		[self _removeAllItems];
		
		// draw the interface.
		[self _draw];
	}
}

- (void)setMessageForWorkingStatus:(NSString *)message {

	// save message value.
	_message = message;
	
	// let's make sure we're on that status.
	if (_status == RequestStatusWorking) {
		
		// on working status, last object it's message label.
		UILabel * label = [[self subviews] lastObject];
		
		// update message.
		label.text = _message;
		
		// resize label.
        [self _resizeLabel:label];
		
		// resize view.
		[self _resizeView];
	}
}

#pragma mark -
#pragma mark Private methods

- (void)_draw {

	// default rect size.
	CGRect frame = CGRectMake(0.0, 0.0, 0.0, 21.0);
	
	// draw objects into view depending of the state.
	if (_status == RequestStatusWorking) {
		
		// add spinner.
		UIActivityIndicatorView * spinner = [[UIActivityIndicatorView alloc]
											 initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
		[spinner startAnimating];
		
		[self _addItem:spinner];
		
		// message label.
		UILabel * messageLabel = [[UILabel alloc] initWithFrame:frame];
		
		// default label customization.
		[self _customizeLabel:messageLabel];
		
		// label customization.
		messageLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
		
		[self _addItem:messageLabel];
	}
	else if (_status == RequestStatusUpdated) {
		
		NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
		
		// updated label.
		UILabel * updatedLabel = [[UILabel alloc] initWithFrame:frame];
		
		// default label customization.
		[self _customizeLabel:updatedLabel];
		
		// label customization.
		updatedLabel.text = @"Updated";
		updatedLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
		
		[self _resizeLabel:updatedLabel];
		[self _addItem:updatedLabel];
		
		// date label.
		UILabel * dateLabel = [[UILabel alloc] initWithFrame:frame];
		
		// default label customization.
		[self _customizeLabel:dateLabel];
		
		// label customization.
		[dateFormat setDateFormat:@"MM/dd/yy"];
		dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
		dateLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
		
		[self _resizeLabel:dateLabel];
		[self _addItem:dateLabel];
		
		// time label.
		UILabel * timeLabel = [[UILabel alloc] initWithFrame:frame];
		
		// default label customization.
		[self _customizeLabel:timeLabel];
		
		// label customization.
		[dateFormat setDateFormat:@"hh:mm"];
		timeLabel.text = [dateFormat stringFromDate:[NSDate date]];
		timeLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
		
		[self _resizeLabel:timeLabel];
		[self _addItem:timeLabel];
	}
	
	// resize view.
	[self _resizeView];
}

- (void)_addItem:(UIView *)view {

	UIView * lastItem = [[self subviews] lastObject];

	// set rect defaults.
	CGRect rect = view.frame;
	
	// check if there's at least an item already added.
	if (lastItem != nil) {
		
		rect.origin.x = lastItem.frame.origin.x + lastItem.frame.size.width + kRequestStatusObjectGap;
	}

	// set view frame.
	[view setFrame:rect];
	
	// add view.
	[self addSubview:view];
}

- (void)_removeAllItems {

	// remove each subview.
	for (UIView * view in [self subviews]) {
		
		[view removeFromSuperview];
	}
}

- (void)_resizeView {

	UIView * lastItem = [[self subviews] lastObject];
	
	CGRect rect = self.frame;
	
	// now change frame values based on last added object.
	rect.size.width = lastItem.frame.origin.x + lastItem.frame.size.width;
	
	// set view frame.
	[self setFrame:rect];
}

- (void)_resizeLabel:(UILabel *)label {
	
	// get size needed for new text.
	CGSize sizeToMakeLabel = [label.text sizeWithFont:label.font];
	
	// resize label.
	label.frame = CGRectMake(label.frame.origin.x,
							 label.frame.origin.y, 
							 sizeToMakeLabel.width,
							 label.frame.size.height);
}

- (void)_customizeLabel:(UILabel *)label {
	
	label.backgroundColor = [UIColor clearColor];
	label.textColor = [UIColor whiteColor];
	label.shadowColor = [UIColor darkGrayColor];
	label.shadowOffset = CGSizeMake(0.0, -1.0);
}

#pragma mark -

- (void)dealloc {
	
	[_message release];
    [super dealloc];
}


@end
